from PyQt6.QtWidgets import QWidget, QPushButton
from PyQt6.uic import loadUi

from ui.BoardPage import BoardPage
from ui.DrawPage import DrawPage


class OriginPage(QWidget):
    def __init__(self):
        super().__init__()
        self.curpage :QWidget= None
        self.content_view: QWidget = None
        self.back_btn: QPushButton = None
        self.board_tab: QPushButton = None
        self.draw_tab: QPushButton = None
        self.video_tab: QPushButton = None

        self.initView()

    def initView(self):
        loadUi('./source/origin_page.ui', self)

        self.board_tab.clicked.connect(
            lambda: self.check_tab_view(0)
        )
        self.draw_tab.clicked.connect(
            lambda: self.check_tab_view(1)
        )
        self.video_tab.clicked.connect(
            lambda: self.check_tab_view(2)
        )
        self.back_btn.clicked.connect(self.on_pressback)

        self.check_tab_view(1)

    def check_tab_view(self, index):
        self.board_tab.setChecked(index == 0)
        self.draw_tab.setChecked(index == 1)
        self.video_tab.setChecked(index == 2)

        def clearview():
            if self.curpage is not None:
                self.content_view.layout().removeWidget(self.curpage)
                self.curpage.deleteLater()
                self.curpage = None

        clearview()
        if index == 0:
            self.curpage = BoardPage()
        elif index == 1:
            self.curpage = DrawPage()
        else:
            pass
        self.content_view.layout().addWidget(self.curpage)

    def on_pressback(self):
        self.parent().initUI()
